package com.qingke.oms.pojo;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="shopping_cart_item")
public class ShoppingCartItem {
	@Id
	@Column(name="id")
	private int id;
	
	@ManyToOne
	@JoinColumn(name="shopping_cart_id")
	private ShoppingCart shoppingCart;
	
	@ManyToOne
	@JoinColumn(name="goods_id")
	private Goods goods;
	
	@Column(name="price")
	private double price;
	
	@Column(name="be_cleared")
	private boolean beCleared;
	
	@OneToMany(mappedBy="shoppingCartItem",fetch=FetchType.LAZY)
	private Set<ItemQuantity> itemQuantities;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public ShoppingCart getShoppingCart() {
		return shoppingCart;
	}
	public void setShoppingCart(ShoppingCart shoppingCart) {
		this.shoppingCart = shoppingCart;
	}
	public boolean isBeCleared() {
		return beCleared;
	}
	public void setBeCleared(boolean beCleared) {
		this.beCleared = beCleared;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getQuantity() {
		int quantity=0;
		if(itemQuantities.size()!=0){
			Date lastestTime=null;
			boolean first=true;
			for(ItemQuantity i:itemQuantities){
				if(first){
					lastestTime=i.getModificationTime();
					quantity=i.getQuantity();
					first=false;
				}
				if(i.getModificationTime().after(lastestTime)){
					lastestTime=i.getModificationTime();
					quantity=i.getQuantity();
				}
			}
			
		}
		return quantity;
	}

	
}
